package com.pets.springboot_petshop.mapper;

import com.pets.springboot_petshop.entity.Appointmentslot;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @Author: wxk
 * @Email: 2089967794@qq.com
 * @Date: 2024/10/14 下午2:55 周一
 * @Description: 预约时间和名额数据访问层接口
 */
@Mapper
@Repository
public interface AppointmentslotMapper {
    //id查询
    @Select("select a.id,a.state,a.service_id,a.appointment_time,a.available_spots,a.deadline,b.original_price,b.discount_price,b.name,b.img,a.create_time,a.update_time" +
            " from appointmentslot a LEFT JOIN service b on a.service_id = b.id where a.id = #{id};")
    Appointmentslot selectappointmentslot(int id);
    //service_id查询
    @Select("select a.id,a.state,a.service_id,a.appointment_time,a.available_spots,a.deadline,b.original_price,b.discount_price,b.name,b.img,a.create_time,a.update_time" +
            " from appointmentslot a LEFT JOIN service b on a.service_id = b.id where a.service_id = #{service_id};")
    List<Appointmentslot> selectservice_id(int service_id);
    @Select("select a.id,a.state,a.service_id,a.appointment_time,a.available_spots,a.deadline,b.original_price,b.discount_price,b.name,b.img,a.create_time,a.update_time" +
            " from appointmentslot a LEFT JOIN service b on a.service_id = b.id where DATE(a.appointment_time) = #{date}")
    List<Appointmentslot> selectOrder(LocalDate date);
    //service_name，通过服务分类查询
    @Select("select a.id,a.state,a.service_id,a.appointment_time,a.available_spots,a.deadline,b.original_price,b.discount_price,b.name,b.img,a.create_time,a.update_time" +
            " from appointmentslot a LEFT JOIN service b on a.service_id = b.id where b.name = #{name};")
    List<Appointmentslot> selectname(String name);
    //查询所有
    @Select("select a.id,a.state,a.service_id,a.appointment_time,a.available_spots,a.deadline,b.original_price,b.discount_price,b.name,b.img,a.create_time,a.update_time" +
            " from appointmentslot a LEFT JOIN service b on a.service_id = b.id")
    List<Appointmentslot> findAll();
    //添加
    @Insert("insert into appointmentslot(service_id,appointment_time,deadline,create_time,update_time,available_spots,state)" +
            "values(#{service_id},#{appointment_time},#{deadline},now(),now(),#{available_spots},#{state});")
    int addAppointmentslot(Appointmentslot appointmentslot);
    //删除
    @Delete("delete from appointmentslot where id=#{id};")
    int deleteAppointmentslotById(int id);
    //修改数据
    @Update("UPDATE appointmentslot SET service_id=#{service_id},appointment_time=#{appointment_time},state=#{state},deadline=#{deadline},available_spots=#{available_spots},update_time=now() where id= #{id};")
    int updateAppointmentslotById(Appointmentslot appointmentslot);
    //修改状态
    @Update("UPDATE appointmentslot SET state=#{state}, update_time=now() WHERE id=#{id}")
    int updatestate(@Param("id") int id, @Param("state") String state);
    //查询状态
    @Select("select * from appointmentslot where state=#{state}")
    List<Appointmentslot> selectstate(String state);
    //查询所有预约名额为0的记录
    @Select("select * from appointmentslot where state='可预约' and available_spots = '0'")
    List<Appointmentslot> selectavailable_spotsstate();
    //查询所以的过期记录
    @Select("SELECT * FROM appointmentslot WHERE deadline < #{now} AND state = '可预约'")
    List<Appointmentslot> findExpiredAppointments(LocalDateTime now);
    // 批量修改status
    @Update({
            "<script>",
            "UPDATE appointmentslot SET state=#{state}, update_time=now() WHERE id IN",
            "<foreach item='item' collection='array' open='(' separator=',' close=')'>",
            "#{item}",
            "</foreach>",
            "</script>"
    })
    int batchUpdateStatus(@Param("array") int[] ids, @Param("state") String state);
    // 批量删除
    @Delete({
            "<script>",
            "DELETE FROM appointmentslot WHERE id IN",
            "<foreach item='item' collection='array' open='(' separator=',' close=')'>",
            "#{item}",
            "</foreach>",
            "</script>"
    })
    int admindelete(@Param("array") int[] ids);
}